/**
 * Created by yangh on 2016/09/04.
 */
var main = {};
var myScroll;
(function ($) {
    main.init = function () {
    	//阻止滑动
    	 
        document.addEventListener('touchmove', function (e) {
            e.preventDefault();
        }, false);
		 
        main.initEvent();
    };

    main.initEvent = function() {
        $(".nav1 li").on("click",function(){
        	//样式
        	$(".nav1 li").removeClass("active");
            $(this).addClass("active");
            
        	//隐藏域
        	var status = $(this).data("id");
        	$("#status").val(status);
        	
        	//加载数据
        	main.loadData(".details",true);
        })
    };

    /**
     * @func move()
     * @desc 监听滚动条的位置，异步加载列表
     */
    function move() {
        if (myScroll.y < main.max - 50) {
        	//加载数据
        	main.loadData(".details",false);
        	
            myScroll.refresh();
            main.max = myScroll.maxScrollY;
            console.log(main.max);
        }
    }
    
    
    /** 
     * function 
     * loadData
     * @param target 	
     * @param replace   replace target 's elements
     */
    main.loadData = function(target,replace){
    	if(replace){
    		$(target).html("");
    	}
    	//ajax begin
    	var html = "";
    	 var status=$("#status").val();
    	 var index = -1;
    	 if($(target+" li").length!=0 && !replace){
    		 index=$(target+" li:last").data("id");
    	 }
         $.ajax({
     		type:"post",
     		async:true, 
        	url:path+"/manage/consumption/more",
        	data:{
        		status:status,
        		index:index
        	},
     		success:function(data){	
     			if('000' == data.code){
     				if(data.result.length==0){
     					$(".tip").hide();
     				}
     				
     				var html = "";
     				for (var i = 0; i < data.result.length; i++) {
     					var order = data.result[i].order;
     					var orderItemList=data.result[i].orderItemList;
     					var infoUrl = path+"/manage/order/"+order.id;
     					var payUrl = path+"/pay/"+order.orderPlat.id;
     					var signUrl = path+"/manage/order/sign/"+order.id;
     					var evaluateUrl = path+"/manage/order/evaluate/"+order.id;
     					
     					html +="<li data-id='"+order.id+"'>";
     					html +="<h4>"+order.merchant.name+"</h4>";
     					for(var j=0; j<orderItemList.length ;j++){
     						var item = orderItemList[j];
     						html +="<a class=\"goods\" href=\"javascript:;\">";
         					html += "   <div class=\"goodsImg\"><img src='"+item.goods.picurl+"' style=\"height: 80px\"></div>";
         					html +="     <h3>"+item.goods.name+"</h3>";
         					html +="     <p class=\"address\">"+item.goods.title+"</p>";
         					html +="    <p class=\"num1\">￥"+item.goods.price+"元</p>";
         					html +=" </a>";
     					}
     					html +="<div class=\"result\">";
     					html +="	<div class=\"amount\">合计：￥"+order.total+"元&nbsp;&nbsp;已支付	</div>";
     					html +="	<div>";
     					if(order.paystatus==1 && order.status!=6  && status==7){
     						html +="<a href='"+evaluateUrl+"' class=\"button pullRight\"  id=\"relation\">评价</a>";
     					};
     					if(order.paystatus==0){
     						html +="<a href='"+payUrl+"' class=\"button pullRight\"  id=\"relation\">付款</a>";
     					};
     					if(order.deliverstatus==1 && order.signstatus ==0 ){
     						html +="<a href='"+signUrl+"' class=\"button pullRight\"  id=\"relation\">签收</a>";
     					};
     					html +="<a href='"+infoUrl+"' class=\"button pullRight\"  id=\"relation\">详情</a>";
     					html +="	</div>";
     					html +="</div>";
     					html +="</li>";
     				}
     				if(data.result.length>=10){
  		            	$("#loading").hide();
		  	            $("#tip").show();
  		            }else{
  		            	$("#loading").hide();
  		            	$("#tip").hide();
  		            }
     				if(replace){
     		    		$(target).html(html);
     		    	}else{
     		    		$(target).append(html);
     		    	}
     				
     				myScroll.refresh();
     	            main.max = myScroll.maxScrollY;
     			}
     		}
     	})
   		//ajax end
    }
    

    /**
     * @func main.scroll()
     * @desc 配置iscroll插件
     */
    main.scroll = function () {
        myScroll = new IScroll("#list", {
            probeType: 3,
            mouseWheel: true,
            click: true,
            preventDefault: false
        });

        main.max = myScroll.maxScrollY;
        console.log(main.max);

        myScroll.on("scroll", move);
    };
})(jQuery);

main.init();
main.scroll();

//加载数据
main.loadData(".details",true);

window.onload = function () {
	
};